//
//  PipeViewController.m
//  Fengshui
//
//  Created by are on 13-5-29.
//
//

#import "PipeViewController.h"
#import "ASIHTTPRequest.h"
@interface PipeViewController ()

@end

@implementation PipeViewController
@synthesize record = _record;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    ABMultiValueRef phone = ABRecordCopyValue(self.record, kABPersonPhoneProperty);
    NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, 0);
    NSString *personname = (NSString *)ABRecordCopyCompositeName(self.record);
    self.nameLabel.text = personname;
    NSRange range=NSMakeRange(0,3);
    NSString *str=[personPhone substringWithRange:range];

    if ([str isEqualToString:@"+86"]) {
        personPhone=[personPhone substringFromIndex:3];
    }
    personPhone = [personPhone stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSString *urlString = [NSString stringWithFormat:@"%@%@",@"http://fengshui22.sinaapp.com/ios/GetUserInfo.php?index_type=uphone_num&index_val=",personPhone];
    
    NSURL *url = [NSURL URLWithString:urlString];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    [request startSynchronous];
    
    NSError *error = [request error];
 
    if (!error) {
        
        NSString *response = [request responseString];
        Dlog(@"%@",response);
        if ([response isEqualToString:@"null"]) {
            self.matchBtn.titleLabel.text = @"邀请";
            self.matchBtn.tag = 0;
            self.resultLabel.text = response;
            Dlog(@"未注册");
        }
        else
        {
            self.matchBtn.titleLabel.text = @"匹配";
            self.matchBtn.tag = 1;
            self.resultLabel.text = response;
        }
    }
    
    //  http://fengshui22.sinaapp.com/ios/GetUserInfo.php?index_type=uphone_num&index_val=13911762456
    // Do any additional setup after loading the view from its nib.

}

- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
    {

        MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
        controller.delegate = self;
        if([MFMessageComposeViewController canSendText])

        {

            controller.body = bodyOfMessage;

            controller.recipients = recipients;

            controller.messageComposeDelegate = self;

            [self presentModalViewController:controller animated:YES];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil
                                                           delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }

    }

    // 处理发送完的响应结果
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
    {
        [self dismissModalViewControllerAnimated:YES];

        if (result == MessageComposeResultCancelled)
            Dlog(@"Message cancelled");
            else if (result == MessageComposeResultSent)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"发送成功" message:nil
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];
            }
                else
                    Dlog(@"Message failed")  ;
    }

-(IBAction)matchBtnAction:(id)sender
{
    if (self.matchBtn.tag == 0) {
      
        ABMultiValueRef phone = ABRecordCopyValue(self.record, kABPersonPhoneProperty);
        NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, 0);
        NSArray *array = [NSArray arrayWithObject:personPhone];
        [self sendSMS:@"我正在用卜卜，算运势非常准哦" recipientList:array];
        
    }
    else if (self.matchBtn.tag == 1)
{
    
}
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
